package com.ss.controller;

import com.alibaba.excel.EasyExcel;
import com.ss.listener.ExcelToInsertSQLListener;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

@RestController
public class ExcelToInsertSQLController {
    /**
     * 功能：上传单个excel文件，解析成SQL
     * 注意1：仅读第一个sheet
     * 注意2：读取的excel表头顺序必须和hive库中的表顺序保持一致
     */
    @PostMapping("uploadExcel")
    public String uploadExcel(MultipartFile file,String savaSQLPath,String dt) throws IOException {
        if(file.isEmpty() || savaSQLPath.isEmpty()){
            return "请将参数填全！";
        }
        if (dt == null){
            // 获取当前日期
            LocalDate today = LocalDate.now();

            // 格式化日期为yyyy-MM-dd
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            dt = today.format(formatter);
        }
        EasyExcel.read(file.getInputStream(), new ExcelToInsertSQLListener(file,savaSQLPath,dt)).sheet().doRead();
        return "转换成功！";
    }
}
